from rest_framework import serializers

from api.conf import order_status
from app.models import FMUser, Order
from app.utils import format_datetime


class UserInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = FMUser
        exclude = ('password',)

    def to_representation(self, instance: FMUser):
        data = super(UserInfoSerializer, self).to_representation(instance)
        not_paid_num = len(Order.objects.filter(user=instance, status=order_status.NOT_PAYED))
        to_be_received_num = len(Order.objects.filter(user=instance, status=order_status.TO_BE_RECEIVED))
        not_shipped_num = len(Order.objects.filter(user=instance, status=order_status.NOT_SHIPPED))
        data.update(dict(order_not_paid_num=not_paid_num,
                         order_to_be_received_num=to_be_received_num,
                         not_shipped_num=not_shipped_num,
                         last_login=format_datetime(instance.last_login),
                         province_name=instance.get_province_display(),
                         city_name=instance.get_city_display(),
                         area_name=instance.get_area_display()))
        return data
